# 协程
# 协程
# 协程
# yield 表示函数是一个生成器 ,通过 next() 方法取值


#
# def t1():
#     yield "a"
#     yield "aaa"
#
#
# def t2():
#     yield "b"
#     yield "bbb"
#
#
# # 主程序入口
# if __name__ == "__main__":
#     z = t1()
#     print(next(z))
#     print(next(z))
#
#     x = t2()
#     print(next(x))
#     print(next(x))
# greenlet 属于手动切换协程 ,遇到io操作程序会阻塞,而不能进行自动切换 ,使用 switch()切换
# from greenlet import greenlet
#
#
# def sing():
#     print("在唱歌")
#     print("唱完歌")
#     d.switch()
#
#
# def dance():
#     print("在跳舞")
#     print("跳完舞")
#     s.switch()
#
#
# if __name__ == "__main__":
#     s = greenlet(sing)
#     d = greenlet(dance)
#     s.switch()
#     d.switch()

# gevent 自动切换协程  遇到io操作会自动切换
# spawn(函数名) 创建协程对象
# sleep() 耗时操作
# join() 阻塞操作 ,等待某个协程执行结束
# joinall() 等待所有的协程结束再退出,参数是一个协程对象列表
# monket 补丁
import gevent


def sing1():
    print("在唱歌1")
    gevent.sleep(5)
    print("唱完歌1")


def dance1():
    print("在跳舞1")
    gevent.sleep(3)
    print("跳完舞1")


if __name__ == "__main__":
    s1 = gevent.spawn(sing1)
    s2 = gevent.spawn(dance1)
    s1.join()
    s2.join()
